﻿#if !NETFX_CORE
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
#endif
#if !(WINDOWS_PHONE || NETFX_CORE)
namespace Nx.Commands
{
    public static class MenuItemClick
    {
        private static readonly DependencyProperty MenuItemClickCommandBehaviorProperty = DependencyProperty.RegisterAttached(
            "ClickCommandBehavior",
            typeof(MenuItemClickCommandBehavior),
            typeof(MenuItemClick),
            null);

        public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
            "Command",
            typeof(ICommand),
            typeof(MenuItemClick),
            new PropertyMetadata(OnSetCommandCallback));

        public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached(
            "CommandParameter",
            typeof(object),
            typeof(MenuItemClick),
            new PropertyMetadata(OnSetCommandParameterCallback));


        public static void SetCommand(UIElement control, ICommand command)
        {
            if (control == null) throw new System.ArgumentNullException("control");
            control.SetValue(CommandProperty, command);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Only works for buttonbase")]
        public static ICommand GetCommand(UIElement control)
        {
            if (control == null) throw new System.ArgumentNullException("control");
            return control.GetValue(CommandProperty) as ICommand;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Only works for buttonbase")]
        public static void SetCommandParameter(UIElement control, object parameter)
        {
            if (control == null) throw new System.ArgumentNullException("buttonBase");
            control.SetValue(CommandParameterProperty, parameter);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Only works for buttonbase")]
        public static object GetCommandParameter(UIElement control)
        {
            if (control == null) throw new System.ArgumentNullException("buttonBase");
            return control.GetValue(CommandParameterProperty);
        }

        private static void OnSetCommandCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            MenuItem control = dependencyObject as MenuItem;
            if (control != null)
            {
                MenuItemClickCommandBehavior behavior = GetOrCreateBehavior(control);
                behavior.Command = e.NewValue as ICommand;
            }
        }

        private static void OnSetCommandParameterCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            MenuItem control = dependencyObject as MenuItem;
            if (control != null)
            {
                MenuItemClickCommandBehavior behavior = GetOrCreateBehavior(control);
                behavior.CommandParameter = e.NewValue;
            }
        }

        private static MenuItemClickCommandBehavior GetOrCreateBehavior(MenuItem control)
        {
            MenuItemClickCommandBehavior behavior = control.GetValue(MenuItemClickCommandBehaviorProperty) as MenuItemClickCommandBehavior;
            if (behavior == null)
            {
                behavior = new MenuItemClickCommandBehavior(control);
                control.SetValue(MenuItemClickCommandBehaviorProperty, behavior);
            }

            return behavior;
        }
    }
}
#endif